package com.widget;

import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

public class BabyView extends View {

	public BabyView(Context context) {
		super(context);
	}
	
	public BabyView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public BabyView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		Paint p = new Paint();
		p.reset();//重置  
        p.setColor(Color.parseColor("#50E19A47"));  
        p.setStyle(Paint.Style.FILL);//设置空心  
        Path path1=new Path();  
        
        if(showWeight)
        {
        	int minStart = (int)(2.7*getHeight()/22);
            int maxStart = (int)(5.5*getHeight()/22);
            
            if(mIndex == 0)
            {
            	path1.moveTo(0, getHeight() - maxStart);  
            	path1.lineTo(getWidth(), getHeight() - (maxStart + (int)(getWidth()*Math.tan(maxAngle))));  
            	path1.lineTo(getWidth(), getHeight() - (minStart + (int)(getWidth()*Math.tan(minAngle))));  
            	path1.lineTo(0, getHeight() - minStart);  
            	path1.lineTo(0, getHeight() - maxStart);  
            }
            else
            {
            	path1.moveTo(0, getHeight() - (maxStart + (int)(mIndex*getWidth()*Math.tan(maxAngle))));  
            	path1.lineTo(getWidth(),getHeight() - (maxStart + (int)(getWidth()*(mIndex+1)*Math.tan(maxAngle)))); 
            	path1.lineTo(getWidth(), getHeight() - (minStart + (int)(getWidth()*(mIndex+1)*Math.tan(minAngle))));
            	path1.lineTo(0, getHeight() - (minStart + (int)(mIndex*getWidth()*Math.tan(minAngle))));
            	path1.lineTo(0,getHeight() - (maxStart + (int)(mIndex*getWidth()*Math.tan(maxAngle))));  
            }
            
            path1.close();//封闭  
            canvas.drawPath(path1, p);
            
            if(pointMap != null)
            {
            	p.setColor(Color.parseColor("#e19a47"));// 设置绿色  
            	p.setStrokeWidth(8); 
            	List<JSONObject> list = (List<JSONObject>)pointMap.get(String.valueOf(mIndex));
            	if(list == null || list.size() == 0) return;
            	
            	if(list.size() == 1)
            	{
            		int yPos = getHeight() - (int)(list.get(0).getDoubleValue("weight_kg")*getHeight()/22);
                    canvas.drawLine(0, yPos, getWidth(), yPos, p);// 斜线  
            	}
            	else if(list.size() == 2)
            	{
            		int yPos0 = getHeight() - (int)(list.get(0).getDoubleValue("weight_kg")*getHeight()/22);
            		int yPos1 = getHeight() - (int)(list.get(1).getDoubleValue("weight_kg")*getHeight()/22);
                    canvas.drawLine(0, yPos0, getWidth(), yPos1, p);// 斜线  
            	}
            	else
            	{
            		int count = list.size();
            		int offset = getWidth()/(count - 1);
            		for(int i = 0; i < count - 1; i++)
            		{
            			int yPos0 = getHeight() - (int)(list.get(i).getDoubleValue("weight_kg")*getHeight()/22);
            			int yPos1 = getHeight() - (int)(list.get(i + 1).getDoubleValue("weight_kg")*getHeight()/22);

            			if(i < count - 2)
            			{
                            canvas.drawLine(offset*i, yPos0, offset*(i + 1), yPos1, p);// 斜线  
            			}
            			else
            			{
                            canvas.drawLine(offset*i, yPos0, getWidth(), yPos1, p);// 斜线  
            			}
            		}
            	}
            }
        }
        else
        {
        	int minStart = (int)(5*getHeight()/55);
            int maxStart = (int)(15*getHeight()/55);
            
            if(mIndex == 0)
            {
            	path1.moveTo(0, getHeight() - maxStart);  
            	path1.lineTo(getWidth(), getHeight() - (maxStart + (int)(getWidth()*Math.tan(maxAngle))));  
            	path1.lineTo(getWidth(), getHeight() - (minStart + (int)(getWidth()*Math.tan(minAngle))));  
            	path1.lineTo(0, getHeight() - minStart);  
            	path1.lineTo(0, getHeight() - maxStart);  
            }
            else
            {
            	path1.moveTo(0, getHeight() - (maxStart + (int)(mIndex*getWidth()*Math.tan(maxAngle))));  
            	path1.lineTo(getWidth(),getHeight() - (maxStart + (int)(getWidth()*(mIndex+1)*Math.tan(maxAngle)))); 
            	path1.lineTo(getWidth(), getHeight() - (minStart + (int)(getWidth()*(mIndex+1)*Math.tan(minAngle))));
            	path1.lineTo(0, getHeight() - (minStart + (int)(mIndex*getWidth()*Math.tan(minAngle))));
            	path1.lineTo(0,getHeight() - (maxStart + (int)(mIndex*getWidth()*Math.tan(maxAngle))));  
            }
             
            path1.close();//封闭  
            canvas.drawPath(path1, p);
            
            if(pointMap != null)
            {
            	p.setColor(Color.parseColor("#e19a47"));// 设置绿色  
            	p.setStrokeWidth(8); 
            	List<JSONObject> list = (List<JSONObject>)pointMap.get(String.valueOf(mIndex));
            	if(list == null || list.size() == 0) return;
            	
            	if(list.size() == 1)
            	{
            		int yPos = getHeight() - (int)((list.get(0).getDoubleValue("height_cm") - 40)*getHeight()/55);
                    canvas.drawLine(0, yPos, getWidth(), yPos, p);// 斜线  
            	}
            	else if(list.size() == 2)
            	{
            		int yPos0 = getHeight() - (int)((list.get(0).getDoubleValue("height_cm") - 40)*getHeight()/55);
            		int yPos1 = getHeight() - (int)((list.get(1).getDoubleValue("height_cm") - 40)*getHeight()/55);
                    canvas.drawLine(0, yPos0, getWidth(), yPos1, p);// 斜线  
            	}
            	else
            	{
            		int count = list.size();
            		int offset = getWidth()/(count - 1);
            		for(int i = 0; i < count - 1; i++)
            		{
            			int yPos0 = getHeight() - (int)((list.get(i).getDoubleValue("height_cm") - 40)*getHeight()/55);
            			int yPos1 = getHeight() - (int)((list.get(i + 1).getDoubleValue("height_cm") - 40)*getHeight()/55);

            			if(i < count - 2)
            			{
                            canvas.drawLine(offset*i, yPos0, offset*(i + 1), yPos1, p);// 斜线  
            			}
            			else
            			{
                            canvas.drawLine(offset*i, yPos0, getWidth(), yPos1, p);// 斜线  
            			}
            		}
            	}
            }
        }
	}
	
	Map pointMap = null;
	int mIndex;
	boolean showWeight = true;
	
	double maxAngle,minAngle;
	
	public void setPoint(int index,Map map,boolean weight,int showIndex)
	{
		pointMap = map;
		mIndex = index;
		showWeight = weight;
		
		 //8 14
        //67  87
		
		if(showIndex == 0)
		{
			if(showWeight)
			{
				maxAngle = 0.17;
				minAngle = 0.10;
			}
			else
			{
				maxAngle = 0.24;
				minAngle = 0.16;
			}
		}
		else if(showIndex == 1)
		{
			if(showWeight)
			{
				maxAngle = 0.038;
				minAngle = 0.025;
			}
			else
			{
				maxAngle = 0.060;
				minAngle = 0.040;
			}
		}
		else
		{
			if(showWeight)
			{
				maxAngle = 0.0055;
				minAngle = 0.0035;
			}
			else
			{
				maxAngle = 0.0085;
				minAngle = 0.0055;
			}
		}
		
		invalidate();
	}
}
